package NewTmas.Listeners;

import NewTmas.FramesandPanels.JobGraphPanel;
import NewTmas.FramesandPanels.LogFileFrame;
import NewTmas.FramesandPanels.TablePanel;
import NewTmas.Popupmenus.TaskPopupMenu;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 7/8/11
 * Time: 12:37 PM
 * To change this template use File | Settings | File Templates.
 */
public class TasksTablePopupListener extends MouseAdapter {
    java.util.List<JobGraphPanel> graphPanels;

    TablePanel tablePanel ;
    JobGraphPanel selectedGraphPanel;

    public TasksTablePopupListener(java.util.List<JobGraphPanel> graphPanels, TablePanel tablePanel){
        this. graphPanels=graphPanels;
        this.tablePanel=tablePanel;

    }

    public void mousePressed(MouseEvent e)
    {
        Check(e);
    }

    public void mouseReleased(MouseEvent e)
    {
        Check(e);
    }


    public void mouseClicked(MouseEvent e){

        if(e.getClickCount()==2 )
        {       JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );
            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);
            Point pt = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), source);
            String task,jobname;
            try{
                task = source.getModel().getValueAt(source.convertRowIndexToModel(row), 1).toString();
                jobname=source.getModel().getValueAt(source.convertRowIndexToModel(row),0).toString();
                if(graphPanels.size()>1) {selectedGraphPanel=  getGraphPanelforjob(jobname) ;}
                else
                {
                    selectedGraphPanel=graphPanels.get(0);
                }

                new LogFileFrame(jobname, task, selectedGraphPanel.date, selectedGraphPanel.dtp) ;
            }catch (IndexOutOfBoundsException ex){

                JOptionPane.showMessageDialog(null,"Invalid row or column selected"+ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);

            }
            e.consume();


        }

    }

    private void Check(MouseEvent e)
    {

        if (e.isPopupTrigger()&& e.getSource() instanceof JTable )
        {
            JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );

            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);
            Point pt = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(),source);
            String task=null,jobname=null;

            task = source.getModel().getValueAt(source.convertRowIndexToModel(row), 1).toString();
            jobname=source.getModel().getValueAt(source.convertRowIndexToModel(row),0).toString();
            if(graphPanels.size()>1) {selectedGraphPanel=  getGraphPanelforjob(jobname) ;}
            else
            {
                selectedGraphPanel=graphPanels.get(0);
            }

            TaskPopupMenu pop=new TaskPopupMenu(selectedGraphPanel, task);
            pop.show(source,pt.x,pt.y);


        }

        e.consume();



    }


    public JobGraphPanel getGraphPanelforjob(String jobName)
    {
        JobGraphPanel selectedPanel=null;
        for(JobGraphPanel j:graphPanels){
            if(j.job.equalsIgnoreCase(jobName))  {
                selectedPanel= j ;
                break;
            }

        }

        return selectedPanel;
    }
}
